import { router } from "@/router";
import { message } from "antd";
import axios from "axios";
import { GET_TOKEN, REMOVE_TOKEN } from "./token";
const request = axios.create({
  timeout: 115000,
  baseURL: "http://geek.itheima.net/v1_0",
  // baseURL: "http://localhost:3000",
})
//interceptors:拦截器
request.interceptors.request.use(config => {
  // if not login add token
  const token = GET_TOKEN()

  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
})

request.interceptors.response.use((response) => {
  
  return response;
}, function (error) {
  switch (error.status) {
    case 401:
     message.error("token失效")
    REMOVE_TOKEN()
    // router.navigate('/login')
      break
    default:
      break
  }

})

export { request }